<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="名称" prop="carnumber">
        <el-input
          v-model="queryParams.carnumber"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="margin_bottom_8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:post:add']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:post:edit']"
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:post:remove']"
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="所属组织" align="center" prop="oid" /> -->
      <el-table-column label="分组" align="center" prop="group" :formatter="row=>row.group.groupname" />
      <el-table-column label="类型" align="center" prop="subType" :formatter="row=>subTypeOptions[row.subType]" />
      <el-table-column label="型号" align="center" prop="model" />
      <el-table-column label="厂家" align="center" prop="factory" />
      <el-table-column label="X轴角度" align="center" prop="pitch" />
      <el-table-column label="Y轴角度" align="center" prop="yaw" />
      <el-table-column label="Z轴角度" align="center" prop="roll" />
      <el-table-column label="三维场景位置" align="center" prop="loacttion" />
      <el-table-column label="三维中显示" align="center" prop="isShow" :formatter="row=>isShowOptions[row.isShow]" />
      <el-table-column label="运行状态" align="center" prop="runState" :formatter="row=>runStateOptions[row.runState]" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:post:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['system:post:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分组" prop="group" required>
          <treeselect v-model="form.group" :options="deptOptions" :normalizer="normalizer" placeholder="选择分组" />
        </el-form-item>
        <el-form-item label="类型" prop="subType" required>
          <el-radio-group v-model="form.subType">
            <el-radio v-for="(item,index) in subTypeOptions" :key="index" :label="index">{{ item }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="型号" prop="model" required>
          <el-input v-model="form.model" placeholder="型号" />
        </el-form-item>
        <el-form-item label="厂家" prop="factory" required>
          <el-input v-model="form.factory" placeholder="厂家" />
        </el-form-item>
        <el-form-item label="X轴角度" prop="pitch" required>
          <el-input-number v-model="form.pitch" controls-position="right" placeholder="X轴角度" />
        </el-form-item>
        <el-form-item label="Y轴角度" prop="yaw" required>
          <el-input-number v-model="form.yaw" controls-position="right" placeholder="Y轴角度" />
        </el-form-item>
        <el-form-item label="Z轴角度" prop="roll" required>
          <el-input-number v-model="form.roll" controls-position="right" placeholder="Z轴角度" />
        </el-form-item>
        <el-form-item label="位置" prop="loacttion" required>
          <el-input v-model="form.loacttion" placeholder="三维场景位置" />
        </el-form-item>
        <el-form-item label="显示" prop="isShow" required>
          <el-radio-group v-model="form.isShow">
            <el-radio v-for="(item,index) in isShowOptions" :key="index" :label="index">{{ item }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="运行状态" prop="runState" required>
          <el-radio-group v-model="form.runState">
            <el-radio v-for="(item,index) in runStateOptions" :key="index" :label="index">{{ item }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addUpdateItem, queryList, deleteById, getById } from '@/api/carmanage/fpd'
import { queryListAll as listGroup } from '@/api/setting/group'
import Treeselect from '@riophae/vue-treeselect'
export default {
  name: 'Post',
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        carnumber: undefined,
        postName: undefined,
        status: undefined
      },
      // 表单参数
      form: { },
      // 表单校验
      rules: {

      },
      deptOptions: [],
      subTypeOptions: ['灭火器', '消防栓', '烟感'],
      isShowOptions: ['不显示', '显示'],
      runStateOptions: ['正常', '故障', '维修']
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true
      queryList(this.queryParams).then(response => {
        this.postList = response.data
        this.total = response.total
        this.loading = false
      })
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      listGroup().then(response => {
        this.deptOptions = this.handleTree(response.data, 'id', 'pid')
      })
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.groupname,
        children: node.children
      }
    },
    // 岗位状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        subType: 0,
        isShow: 1,
        runState: 0
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.getTreeselect()
      this.open = true
      this.title = '添加岗位'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.getTreeselect()
      const id = row.id || this.ids
      getById(id).then(response => {
        this.form = { ...response.data, group: response.data.group.id }
        this.open = true
        this.title = '修改岗位'
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            addUpdateItem({ ...this.form, group: { id: this.form.group }}).then(response => {
              if (response.code === 200) {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              } else {
                this.msgError(response.msg)
              }
            })
          } else {
            addUpdateItem({ ...this.form, group: { id: this.form.group }}).then(response => {
              if (response.code === 200) {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              } else {
                this.msgError(response.msg)
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除岗位编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return deleteById(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(function() {})
    }
    /** 导出按钮操作 */
    // handleExport() {
    //   // const queryParams = this.queryParams
    //   this.$confirm('是否确认导出所有岗位数据项?', '警告', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消',
    //     type: 'warning'
    //   }).then(function() {
    //     // return exportPost(queryParams)
    //   }).then(response => {
    //     this.download(response.msg)
    //   }).catch(function() {})
    // }
  }
}
</script>
